using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Net.SamuelChen.Libs.WinForm.Tricks
{
	/// <summary>
	/// Summary description for Clock.
	/// </summary>
	public class TraditinalClock : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.Timer timerTick;
		private System.ComponentModel.IContainer components;

		private DateTime _activeTime = DateTime.Now;

		PointF ptCenter;
		int radius;
		double radian = 2 * Math.PI / 60;

		public TraditinalClock()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			// Set styles
			this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
			this.SetStyle(ControlStyles.ResizeRedraw, true);
		}

		public void Start() {
			timerTick.Start();
		}

		public void Stop() {
			timerTick.Stop();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.timerTick = new System.Windows.Forms.Timer(this.components);
			// 
			// timerTick
			// 
			this.timerTick.Enabled = true;
			this.timerTick.Interval = 1000;
			this.timerTick.Tick += new System.EventHandler(this.timerTick_Tick);
			// 
			// TraditinalClock
			// 
			this.Name = "TraditinalClock";

		}
		#endregion


		/* -------------------------------
		 * ---------- events -------------
		 * -------------------------------
		 */

		protected override void OnPaint(PaintEventArgs e) {
			base.OnPaint (e);

			Graphics graph = e.Graphics;

			// draw hour points
			PointF ptHour = new PointF();
			PointF ptShadow = new PointF();
			for (int i=0; i<60; i+=5) {
				ptHour.X = ptCenter.X + (float)(radius * Math.Sin(i*radian)) - 1;
				ptShadow.X = ptHour.X + 1;
				ptHour.Y = ptCenter.Y - (float)(radius * Math.Cos(i*radian)) - 1;
				ptShadow.Y = ptHour.Y ;
				
				graph.FillEllipse(Brushes.Aqua, ptHour.X, ptHour.Y, 2, 2);
				graph.FillEllipse(Brushes.CadetBlue, ptShadow.X, ptShadow.Y, 3, 3);

//				if (i % 5 == 0) {
//					graph.FillEllipse(Brushes.Aqua, ptHour.X, ptHour.Y, 2, 2);
//					graph.FillEllipse(Brushes.CadetBlue, ptShadow.X, ptShadow.Y, 3, 3);
//				} else
//					graph.FillEllipse(Brushes.Black, ptShadow.X, ptShadow.Y, 1, 1);
			}

			DrawMiniutePointer(graph);
			DrawHourPointer(graph);
			DrawSecondPointer(graph);
		}

		protected override void OnResize(EventArgs e) {
			ptCenter = new PointF(this.Width/2 - 5, this.Height/2 - 5);
			radius = this.Width > this.Height ? (this.Height/2 - 10) : (this.Width/2 - 10);

			base.OnResize (e);
		}

		private void timerTick_Tick(object sender, System.EventArgs e) {
			ActiveTime = DateTime.Now;
			this.Invalidate();
		}

		/* -------------------------------
		 * --------- properties ----------
		 * -------------------------------
		 */

		public DateTime ActiveTime {
			get {
				return _activeTime;
			}
			set {
				_activeTime = value;
			}
		}

		/* -------------------------------
		 * ------ private methods --------
		 * -------------------------------
		 */

		private void DrawSecondPointer(Graphics graph) {
			// get radian coefficient
			PointF ptEnd = new PointF();
			ptEnd.X = ptCenter.X + (float)((radius - 10) * Math.Sin(_activeTime.Second * radian));
			ptEnd.Y = ptCenter.Y - (float)((radius - 10) * Math.Cos(_activeTime.Second * radian));
			graph.DrawLine(Pens.DarkGray, ptCenter, ptEnd);
		}

		private void DrawHourPointer(Graphics graph) {
			// get radian coefficient
			PointF ptEnd = new PointF();
			PointF ptLeft = new PointF();
			PointF ptRight = new PointF();
			PointF ptStart = new PointF();

			ptEnd.X = ptCenter.X + (float)((radius - 15) * Math.Sin(_activeTime.Hour * radian));
			ptEnd.Y = ptCenter.Y - (float)((radius - 15) * Math.Cos(_activeTime.Hour * radian));

			ptLeft.X = ptCenter.X + (float)((radius - 15) / 5 * Math.Sin((_activeTime.Hour - 2) * radian));
			ptLeft.Y = ptCenter.Y - (float)((radius - 15) / 5 * Math.Cos((_activeTime.Hour - 2) * radian));

			ptRight.X = ptCenter.X + (float)((radius - 15) / 5 * Math.Sin((_activeTime.Hour + 2) * radian));
			ptRight.Y = ptCenter.Y - (float)((radius - 15) / 5 * Math.Cos((_activeTime.Hour + 2) * radian));

			ptStart.X = ptCenter.X - (float)(6 * Math.Sin(_activeTime.Hour * radian));
			ptStart.Y = ptCenter.Y + (float)(6 * Math.Cos(_activeTime.Hour * radian));

			graph.FillPolygon(Brushes.CadetBlue, new PointF[]{ptStart, ptLeft, ptEnd, ptRight});
			graph.DrawPolygon(Pens.GhostWhite, new PointF[]{ptStart, ptLeft, ptEnd, ptRight});
		}

		private void DrawMiniutePointer(Graphics graph) {
			// get radian coefficient
			PointF ptEnd = new PointF();
			PointF ptLeft = new PointF();
			PointF ptRight = new PointF();
			PointF ptStart = new PointF();

			ptEnd.X = ptCenter.X + (float)((radius - 10) * Math.Sin(_activeTime.Minute * radian));
			ptEnd.Y = ptCenter.Y - (float)((radius - 10) * Math.Cos(_activeTime.Minute * radian));

			ptLeft.X = ptCenter.X + (float)((radius - 10) / 5 * Math.Sin((_activeTime.Minute - 2) * radian));
			ptLeft.Y = ptCenter.Y - (float)((radius - 10) / 5 * Math.Cos((_activeTime.Minute - 2) * radian));

			ptRight.X = ptCenter.X + (float)((radius - 10) / 5 * Math.Sin((_activeTime.Minute + 2) * radian));
			ptRight.Y = ptCenter.Y - (float)((radius - 10) / 5 * Math.Cos((_activeTime.Minute + 2) * radian));

			ptStart.X = ptCenter.X - (float)(10 * Math.Sin(_activeTime.Minute * radian));
			ptStart.Y = ptCenter.Y + (float)(10 * Math.Cos(_activeTime.Minute * radian));

			//			PointF ptEndShadow = new PointF();
			//			PointF ptLeftShadow = new PointF();
			//			PointF ptRightShadow = new PointF();
			//			PointF ptStartShadow = new PointF();			
			//
			//			ptEndShadow.X = ptCenter.X + (float)((radius - 5.12) * Math.Sin(_activeTime.Minute * radian));
			//			ptEndShadow.Y = ptCenter.Y - (float)((radius - 5.12) * Math.Cos(_activeTime.Minute * radian));
			//
			//			ptLeftShadow.X = ptCenter.X + (float)((radius - 5.12) / 5 * Math.Sin((_activeTime.Minute - 2) * radian));
			//			ptLeftShadow.Y = ptCenter.Y - (float)((radius - 5.12) / 5 * Math.Cos((_activeTime.Minute - 2) * radian));
			//
			//			ptRightShadow.X = ptCenter.X + (float)((radius - 5.12) / 5 * Math.Sin((_activeTime.Minute + 2) * radian));
			//			ptRightShadow.Y = ptCenter.Y - (float)((radius - 5.12) / 5 * Math.Cos((_activeTime.Minute + 2) * radian));
			//
			//			ptStartShadow.X = ptCenter.X - (float)(10 * Math.Sin((_activeTime.Minute + 2) * radian));
			//			ptStartShadow.Y = ptCenter.Y + (float)(10 * Math.Cos((_activeTime.Minute + 2) * radian));

			//graph.FillPolygon(Brushes.DarkGray, new PointF[]{ptStartShadow, ptLeftShadow, ptEndShadow, ptRightShadow});
			graph.FillPolygon(Brushes.CadetBlue, new PointF[]{ptStart, ptLeft, ptEnd, ptRight});
		}

	}
}
